package com.yifeng.repo.base.dto;

import com.yifeng.repo.base.constant.BaseConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;

/**
 * Created by daibing on 2023/8/23.
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDto {

    /**
     * id
     */
    private String id;

    /**
     * 工号
     */
    private String code;

    /**
     * 姓名
     */
    private String name;

    /**
     * 手机号码
     */
    private String mobile;

    /**
     * 归属组织编码
     */
    private String orgCode;

    /**
     * 归属门店编号
     */
    private String storeCode;

    /**
     * 发行日期
     */
    private Long iat;

    /**
     * 过期日期
     */
    private Long exp;

    /**
     * 2B：账号权限
     */
    public UserDto(String code, String name) {
        this.code = code;
        this.name = name;
        // 兼容逻辑：组织和门店设置空
        this.orgCode = BaseConstant.EMPTY;
        this.storeCode = BaseConstant.EMPTY;
        this.iat = LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond();
        this.exp = LocalDateTime.of(LocalDate.now().plusDays(7), LocalTime.of(6, 0))
                .atZone(ZoneId.systemDefault())
                .toEpochSecond();
    }

    /**
     * 2C：会员权限
     */
    public UserDto(String id, String code, String name, String mobile) {
        this.id = id;
        this.code = code;
        this.name = name;
        this.mobile = mobile;
        this.iat = LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond();
        this.exp = LocalDateTime.of(LocalDate.now().plusDays(7), LocalTime.of(6, 0))
                .atZone(ZoneId.systemDefault())
                .toEpochSecond();
    }

    public boolean isExpired() {
        return this.exp <= LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond();
    }

    public static UserDto newBusinessUser(String code, String name, String orgCode, String storeCode) {
        UserDto userDto = new UserDto();
        userDto.setCode(code);
        userDto.setName(name);
        userDto.setOrgCode(orgCode);
        userDto.setStoreCode(storeCode);
        userDto.setIat(LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond());
        userDto.setExp(LocalDateTime.of(LocalDate.now().plusDays(7), LocalTime.of(6, 0))
                .atZone(ZoneId.systemDefault())
                .toEpochSecond());
        return userDto;
    }

    public static UserDto newCustomerUser(String id, String code, String name, String mobile) {
        UserDto userDto = new UserDto();
        userDto.setId(id);
        userDto.setCode(code);
        userDto.setName(name);
        userDto.setMobile(mobile);
        userDto.setIat(LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond());
        userDto.setExp(LocalDateTime.of(LocalDate.now().plusDays(7), LocalTime.of(6, 0))
                .atZone(ZoneId.systemDefault())
                .toEpochSecond());
        return userDto;
    }

}
